<template>
  <div>
    <el-container>
      <el-main>
        <div style="margin-bottom:15px;text-align: left">
          <el-input style="width: 240px" placeholder="站点名称" size="small" v-model="params.license"></el-input>
          <el-input style="width: 240px; margin-left: 15px" placeholder="站点电话" size="small" v-model="params.department"></el-input>
          <el-button style="margin-left: 15px" size="small" type="primary" @click="getall"><i class="el-icon-search"></i> 搜索</el-button>
          <el-button style="margin-left: 15px" size="small" type="warning" @click="reset"><i class="el-icon-refresh"></i> 重置</el-button>
        </div>
        <div style="text-align: left" >
          <el-button style="margin-left: 0px" size="small" type="primary" @click="addinfo"><i class="el-icon-circle-plus-outline"></i>     添加信息</el-button>
        </div>
        <el-table
            :border="true"
            :data="tableData"
            stripe
            style="width: 100%;margin-top: 20px">
          <el-table-column
              prop="id"
              label="编号"
              width="60">
          </el-table-column>
          <el-table-column
              prop="license"
              label="车牌号"
              width="150">
          </el-table-column>
          <el-table-column
              prop="department"
              label="部门"
              width="240">
          </el-table-column>
          <el-table-column
              prop="status"
              label="状态(支持筛选)"
              width="150"
              :filters="[{ text: '开放中', value: '开放中' }, { text: '未开放', value: '未开放' }]"
              :filter-method="filterTag"
              filter-placement="bottom-end">
            <template slot-scope="scope">
              <el-tag
                  :type="scope.row.status === '开放中' ? 'primary' : 'success'"
                  disable-transitions>{{scope.row.status === '开放中' ? "开放中":"未开放" }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
              prop="model"
              label="型号"
              width="240">
          </el-table-column>
          <el-table-column
              prop="producter"
              label="生产厂家"
              width="180">
          </el-table-column>
          <el-table-column
              prop="registerTime"
              label="注册时间"
              width="210">
          </el-table-column>
          <el-table-column label="操作"  width="200">
            <template #default="scope">
              <!--                        scope.row 就是当前行数据-->
              <el-button type="primary" size="small" @click="handleEdit(scope.row)"><i class="el-icon-edit-outline"></i>  编 辑</el-button>
              <el-popconfirm
                  style="margin-left: 5px"
                  title="您确定删除这行数据吗？"
                  @confirm="del(scope.row.id)"
              >
                <el-button type="danger" size="small" slot="reference"><i class="el-icon-delete"></i>  删 除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!--    分页-->
        <div style="margin-top: 20px">
          <el-pagination
              background
              :current-page="params.pageNum"
              :page-size="params.pageSize"
              layout="prev, pager, next, jumper"
              @current-change="handleCurrentChange"
              :total="total">
          </el-pagination>
        </div>
        <!--      修改-->
        <div style="margin: 0px 0">
          <el-dialog
              :visible.sync="centerDialogVisibleEdit"
              title="修改"
              width="30%"
              center>
            <el-form :model="form" ref="ruleForm" :rules="rules" label-width="80px">
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="车牌">
                    <el-input v-model="form.license" style="width: 120%" disabled/>
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="生产厂家">
                    <el-input v-model="form.producter" style="width: 79%;" placeholder="请输入"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="部门">
                <el-select v-model="form.department" style="width: 90%" placeholder="请选择">
                  <el-option
                      v-for="item in departments"
                      :value="item">
                    {{ item }}
                  </el-option>
                </el-select>
              </el-form-item>
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="型号">
                    <el-input v-model="form.model" style="width: 90%" placeholder="型号"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="状态">
                    <el-select v-model="form.status" style="width: 72%" placeholder="请选择">
                      <el-option
                          v-for="item in statuses"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="注册时间">
                <el-input v-model="form.registerTime" style="width: 90%;" placeholder="注册时间" disabled/>
              </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                  <el-button @click="centerDialogVisibleEdit = false">取消</el-button>
                  <el-button type="primary" @click="save">确认</el-button>
                </span>
            </template>
          </el-dialog>
        </div>
        <!-- 添加 -->
        <div style="margin: 0px 0">
          <el-dialog
              :visible.sync="centerDialogVisibleAdd"
              title="添加"
              width="30%"
              center>
            <el-form :model="addform" ref="ruleForm" :rules="rules" label-width="80px">
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="车牌">
                    <el-input v-model="addform.license" style="width: 120%" placeholder="请输入"/>
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="生产厂家">
                    <el-input v-model="addform.producter" style="width: 79%;" placeholder="请输入"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="部门">
                <el-select v-model="addform.department" style="width: 90%" placeholder="请选择">
                  <el-option
                      v-for="item in departments"
                      :value="item">
                    {{ item }}
                  </el-option>
                </el-select>
              </el-form-item>
              <el-row :gutter="20">
                <el-col :span="10">
                  <el-form-item label="型号">
                    <el-input v-model="addform.model" style="width: 120%" placeholder="型号"/>
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="状态">
                    <el-select v-model="addform.status" style="width: 79%" placeholder="请选择">
                      <el-option
                          v-for="item in statuses"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        <span style="float: left">{{ item.label }}</span>
                        <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="注册时间">
                <el-input v-model="addform.registerTime" style="width: 90%;" placeholder="无需填写" disabled/>
              </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                  <el-button @click="centerDialogVisibleAdd = false">取消</el-button>
                  <el-button type="primary" @click="confirmadd">确认</el-button>
                </span>
            </template>
          </el-dialog>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  data(){
    return{
      tableData: [],
      total:0,
      params: {
        pageNum: 1,
        pageSize: 7,
        license: '',
        department: ''
      },
      departments:[], //下拉菜单
      statuses:[{
        value: 'opening',
        label: '开放中'
      }, {
        value: 'closed',
        label: '未开放'
      }],
      form:{
        license: '',
        department:'',
        model:'',
        producter:'',
        registerTime:'',
        status:''
      },
      addform:{
        license: '',
        department:'',
        model:'',
        producter:'',
        registerTime:'',
        status:''
      },
      rules: {
        license: [{required: true, message: '请输入车牌', trigger: 'blur'}],
        department: [{required: true, message: '请输入部门', trigger: 'blur'}],
      },
      centerDialogVisibleEdit:false, //编辑弹窗
      centerDialogVisibleAdd:false, //添加弹窗
    }
  },
  mounted() {
  },
  created() {
    this.getall()
    this.getOptions()
  },
  methods:{
    getall(){
      request.get('cars/page',{
        params: this.params
      }).then(res=>{
        if(res.code === '200'){
          this.tableData = res.data.list
          this.total=res.data.total
        }
      })
    },
    async getOptions() {
      request.get('sites/allsites',{

      }).then(res=>{
        if(res.code === '200'){
          this.departments = res.data;
        }
      })
    },
    reset(){
      this.params = {
        pageNum: 1,
        pageSize: 7,
        donors: '',
        bloodType: ''
      }
      this.getall()
    },
    del(id){
      request.delete('cars/deleteOne/'+id).then(res=>{
        if (res.code === '200') {
          this.$notify.success('删除成功')
          this.getall()
        } else {
          this.$notify.error(res.msg)
        }
      })
    },
    confirmadd(){
      request.post('cars/addOne',this.addform).then(res=> {
        if (res.code === '200') {
          this.$message({
            type: "success",
            message: "添加信息成功！"
          })
          this.addform={}
          this.centerDialogVisibleAdd = false
          this.getall()
        } else {
          this.$message({
            type: "warning",
            message: res.msg
          })
          this.addform={}
          this.centerDialogVisibleAdd = false
          this.getall()
        }
      })
    },
    async save(){
      this.form.optioner=JSON.parse(localStorage.getItem('name')).name
      if(this.form.id){
        this.$refs['ruleForm'].validate((valid) => {
          if(valid){
            request.put('cars/updateOne',this.form).then(res=>{
              if(res.code === '200'){
                this.$message({
                  type:"success",
                  message:"更新信息成功！"
                })
                this.centerDialogVisibleEdit=false
                this.getall()
              }else{
                this.$message({
                  type:"warning",
                  message: res.msg
                })
              }
            })
          }
        })
      }
    },
    handleCurrentChange(pageNum) {
      // 点击分页按钮触发分页
      this.params.pageNum = pageNum
      this.getall()
    },
    handleEdit(row){
      this.form=JSON.parse(JSON.stringify(row))
      this.centerDialogVisibleEdit=true
    },
    addinfo(){
      this.centerDialogVisibleAdd=true
    },
    filterTag(value, row) {
      return row.type === value;
    }
  }
}
</script>

<style>

</style>